//
//  OKSickDetailController.m
//  fast
//
//  Created by answer on 16/5/21.
//  Copyright © 2016年 answer. All rights reserved.
//

#import "OKSickDetailController.h"
#import <Masonry/Masonry.h>
#import "OKSelectBtn.h"
#import "OKTreatBtn.h"
#import "OKButton.h"
#import "LSHTTPManager.h"
#import "LSAPIManager.h"
#import <YYModel/YYModel.h>
#import "OKSickModel.h"
#import "OKSickSearchController.h"
#import "OKSickComplicationController.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "OKSickTreatController.h"
#import "OKDoctorSelectController.h"


@interface OKSickDetailController () <OKSickSearchControllerDelegate, OKSickComplicationControllerDelegate, OKSickTreatControllerDelegate>
@property (assign, nonatomic) NSInteger treatType;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *leftTopView;
@property (strong, nonatomic) UILabel *rightTopView;
@property (strong, nonatomic) OKSelectBtn *firstBtn;
@property (strong, nonatomic) OKSelectBtn *secondBtn;

@property (strong, nonatomic) OKButton *thirdBtn;
@property (strong, nonatomic) OKTreatBtn *btn1;
@property (strong, nonatomic) OKTreatBtn *btn2;

@property (strong, nonatomic) OKButton *fourBtn;
@property (strong, nonatomic) OKTreatBtn *btn3;
@property (strong, nonatomic) OKTreatBtn *btn4;

@property (strong, nonatomic) OKSelectBtn *fiveBtn;
@property (strong, nonatomic) OKTreatBtn *btn5;
@property (strong, nonatomic) OKTreatBtn *btn6;

// 底部子控件
@property (strong, nonatomic) UIButton *bottomBtn;
@property (strong, nonatomic) UIImageView *iconView;
@property (strong, nonatomic) UILabel *doctorNumLabel;
@property (strong, nonatomic) UILabel *matchTitleLabel;
@end

@implementation OKSickDetailController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.fiveBtn.hidden = YES;
    
    self.bottomBtn.enabled = NO;
    
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];
    [self setupUI];
    self.navigationItem.title = @"病情详细选择";
    
}


#pragma mark - 添加子空间及约束
- (void)setupUI {
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.firstBtn];
    [self.view addSubview:self.secondBtn];
    [self.view addSubview:self.thirdBtn];
    [self.view addSubview:self.fourBtn];
    [self.view addSubview:self.fiveBtn];
    [self.view addSubview:self.btn1];
    [self.view addSubview:self.btn2];
    [self.view addSubview:self.btn3];
    [self.view addSubview:self.btn4];
    [self.view addSubview:self.bottomBtn];
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.doctorNumLabel];
    [self.view addSubview:self.matchTitleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(12);
        make.top.equalTo(self.view).offset(18);
    }];
    
    [self.firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(12);
        make.right.equalTo(self.view).offset(-12);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(26);
        make.height.equalTo(@35);
    }];
    
    [self.secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.firstBtn);
        make.top.equalTo(self.firstBtn.mas_bottom).offset(8);
        make.height.equalTo(self.firstBtn);
    }];
    
    [self.thirdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.firstBtn);
        make.top.equalTo(self.secondBtn.mas_bottom).offset(8);
        make.height.equalTo(@35);
        
    }];
    // 给thirdBtn添加子控件
    [self.btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.thirdBtn.mas_right).offset(-20);
        make.centerY.equalTo(self.thirdBtn);
    }];
    
    [self.btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.btn2.mas_left).offset(-20);
        make.centerY.equalTo(self.thirdBtn);
    }];
    
    [self.fourBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.firstBtn);
        make.top.equalTo(self.thirdBtn.mas_bottom).offset(8);
        make.height.equalTo(@35);
    }];
    // 给fourBtn添加子控件
    [self.btn4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.fourBtn.mas_right).offset(-20);
        make.centerY.equalTo(self.fourBtn);
    }];
    [self.btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.btn4.mas_left).offset(-20);
        make.centerY.equalTo(self.fourBtn);
    }];
    
    [self.fiveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.firstBtn);
        make.top.equalTo(self.fourBtn.mas_bottom).offset(8);
        make.height.equalTo(@35);
    }];
    
    // 添加底部视图
    [self.bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(12);
        make.right.equalTo(self.view).offset(-12);
        make.bottom.equalTo(self.view).offset(-20);
        make.height.equalTo(@44);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.bottomBtn.mas_top).offset(-16);
    }];
    
    [self.doctorNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.iconView.mas_top).offset(-13.5);
    }];
    
    [self.matchTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.doctorNumLabel.mas_top).offset(-16);
    }];
}

#pragma mark - 上部视图控件懒加载
- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = [NSString stringWithFormat:@"疾病类型: %@", self.sickType];
    }
    return _titleLabel;
}

- (OKSelectBtn *)firstBtn {
    if (_firstBtn == nil) {
        _firstBtn = [[OKSelectBtn alloc] init];
        [_firstBtn setTitle:@"请选择疾病细分" forState:UIControlStateNormal];
        [_firstBtn addTarget:self action:@selector(firstBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _firstBtn;
}

#pragma mark - 疾病细分按钮点击
- (void)firstBtnClick:(OKSelectBtn *)btn {
    
   
    /*
     请求地址: http://iosapi.itcast.cn/doctor/searchCI3List.json.php
     请求参数:{"page":1,"page_size":15,"ci1_id":1,"keyword":""}
     */
    NSString *ci1_id = [NSString stringWithFormat:@"%zd", self.ci1_id];
    NSDictionary *params = @{@"page":@1,
                             @"page_size":@15,
                             @"ci1_id":ci1_id,
                             @"keyword":@""
                             };
    //   创建跳转控制器
    OKSickSearchController *searchController = [[OKSickSearchController alloc] init];
    searchController.params = params;
    searchController.delegate = self;
    [self.navigationController pushViewController:searchController animated:YES];
    
    
}

#pragma mark - 代理回调
- (void)sickSegmentCellClick:(OKSickModel *)model {
    
   
    self.ci2_id = model.ci2_id;
    self.ci3_id = model.ci3_id;
    
    [self.firstBtn setTitle:model.ci3_name forState:UIControlStateNormal];
    self.firstBtn.selected = YES;
    if (self.firstBtn.selected && self.thirdBtn.selected && self.fourBtn.selected) {
        [self doctorCountRequest];
    }
    [self.firstBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    
}

#pragma mark - 疾病并发按钮点击(可多选)

- (OKSelectBtn *)secondBtn {
    if (_secondBtn == nil) {
        _secondBtn = [[OKSelectBtn alloc] init];
        [_secondBtn setTitle:@"请选择并发症(可多选)" forState:UIControlStateNormal];
        [_secondBtn addTarget:self action:@selector(secondbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _secondBtn;
}

- (void)secondbtnClick:(OKSelectBtn *)btn {
    
    //  判断疾病细分按钮是否已选
    if (self.firstBtn.selected == NO) {
        [SVProgressHUD showErrorWithStatus:@"请先选择疾病细分"];
        return;
    }
    
    //请求地址: http://iosapi.itcast.cn/doctor/complicationList.json.php
    //请求参数:{"page":1,"page_size":15,"ci1_id":1,"ci2_id":3}
    NSString *ci1_id = [NSString stringWithFormat:@"%zd", self.ci1_id];
    NSString *ci2_id = [NSString stringWithFormat:@"%zd", self.ci2_id];
    NSDictionary *params = @{@"page":@1,
                             @"page_size":@15,
                             @"ci1_id":ci1_id,
                             @"ci2_id":ci2_id
                             };
    
    OKSickComplicationController *sickComplication = [[OKSickComplicationController alloc] init];
    sickComplication.params = params;
    sickComplication.delegate = self;
    [self.navigationController pushViewController:sickComplication animated:YES];
}

- (void)sickComplicationControllerCallBack:(NSArray *)result {
    
    self.secondBtn.selected = YES;
    NSMutableString *str = [NSMutableString stringWithFormat:@"["];
    
    for (NSString *sickName in result) {
        
        [str appendString:sickName];
        [str appendString:@", "];
    }
    NSString *count = [NSString stringWithFormat:@"[共选择:%zd个]", result.count];
    [str appendString:@"]"];
    [str appendString:count];
    
    [self.secondBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    [self.secondBtn setTitle:str forState:UIControlStateSelected];
    
    
    // 添加富文本属性
    NSRange range = [str rangeOfString:count];
    // 给高亮字添加属性
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:str];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:[UIColor greenColor]
                          range:range];
    
    
    [self.secondBtn setAttributedTitle:AttributedStr forState:UIControlStateSelected];

    
}

- (OKButton *)thirdBtn {
    if (_thirdBtn == nil) {
        _thirdBtn = [OKButton buttonWithTitle:@"是否确证"];
    }
    return _thirdBtn;
}

- (OKButton *)fourBtn {
    if (_fourBtn == nil) {
        _fourBtn = [OKButton buttonWithTitle:@"是否接受过治疗"];
    }
    return _fourBtn;
}

- (OKSelectBtn *)fiveBtn {
    if (_fiveBtn == nil) {
        _fiveBtn = [[OKSelectBtn alloc] init];
        [_fiveBtn setTitle:@"请选择治疗方式" forState:UIControlStateNormal];
        [_fiveBtn addTarget:self action:@selector(fiveBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _fiveBtn;
}

#pragma mark - 按钮点击事件
- (void)fiveBtnClick {
    OKSickTreatController *vc = [[OKSickTreatController alloc] init];
    vc.delegate = self;
    vc.data = @[@"手术治疗", @"保守治疗", @"药物治疗"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)surebtnClickCallBack:(NSInteger)treatType {
    self.fiveBtn.selected = YES;
    
    self.treatType = treatType;
    [self.fiveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
//    NSLog(@"%zd",treatType);
    switch (treatType) {
        case 2:
            [self.fiveBtn setTitle:@"手术治疗" forState:UIControlStateSelected];
            break;
        case 3:
            [self.fiveBtn setTitle:@"保守治疗" forState:UIControlStateSelected];
            break;
        case 4:
            [self.fiveBtn setTitle:@"药物治疗" forState:UIControlStateSelected];
            break;
    }
}

- (OKTreatBtn *)btn1 {
    if (_btn1 == nil) {
        _btn1 = [OKTreatBtn buttonWithTitle:@"已确诊"];
        [_btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn1;
}
- (OKTreatBtn *)btn2 {
    if (_btn2 == nil) {
        _btn2 = [OKTreatBtn buttonWithTitle:@"症状疑似"];
        [_btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn2;
}

- (OKTreatBtn *)btn3 {
    if (_btn3 == nil) {
        _btn3 = [OKTreatBtn buttonWithTitle:@"接受过"];
        [_btn3 addTarget:self action:@selector(bottomBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn3;
}
- (OKTreatBtn *)btn4 {
    if (_btn4 == nil) {
        _btn4 = [OKTreatBtn buttonWithTitle:@"未接受过"];
        [_btn4 addTarget:self action:@selector(bottomBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn4;
}

#pragma mark - 疾病按钮点击跳转


#pragma mark - 确证及治疗按钮子按钮点击
- (void)btnClick:(OKTreatBtn *)btn {
    
    self.thirdBtn.selected = YES;
    
    self.btn5.selected = NO;
    btn.selected = YES;
    self.btn5 = btn;
    
    if (self.firstBtn.selected && self.thirdBtn.selected && self.fourBtn.selected) {
        [self doctorCountRequest];
    }
}

- (void)bottomBtnClick:(OKTreatBtn *)btn {
    
    self.fourBtn.selected = YES;
    
    if (btn == self.btn3) {
        self.fiveBtn.hidden = NO;
    } else {
        self.fiveBtn.hidden = YES;
    }
    
    self.btn6.selected = NO;
    btn.selected = YES;
    self.btn6 = btn;
    
    if (self.firstBtn.selected && self.thirdBtn.selected && self.fourBtn.selected) {
        [self doctorCountRequest];
    }
}

#pragma mark - 监听按钮点击一旦用户选择好, 自动请求医生数据
- (void)doctorCountRequest {
    //请求地址: http://iosapi.itcast.cn/doctor/matchedDoctorCount.json.php
    //请求参数:
    //{"ci1_id":1,"ci2_id":3,"ci3_id":3,"diagnosis_type":0,"is_confirmed":1,"user_id":1000089, "has_diagnosis":2}
    //int diagnosis_type;  2-手术治疗   3 – 保留治疗  4 –药物治疗
    //int is_confirmed;//是否已确诊  /** 是否就诊过 1-是 2-否*/
    //user_id "user_id":1000089,
    // "has_diagnosis":   /** 是否接受过治疗 1-是 2-否*/
        
    NSDictionary *params = @{@"ci1_id":@1,
                             @"ci2_id":@3,
                             @"ci3_id":@3,
                             @"diagnosis_type":@(self.treatType),
                             @"is_confirmed":@(1),
                             @"user_id":@1000089,
                             @"has_diagnosis":@(2)
                             };
 
    
    [[LSHTTPManager sharedManager] POST:@"doctor/matchedDoctorCount.json.php" parameters:params comletionHandle:^(id responseObject, NSError *error) {
        //NSLog(@"%@", responseObject);
        self.doctorNumLabel.text = @"24  位";
        
    }];
    [self.bottomBtn setBackgroundColor:[UIColor colorWithRed:50/255.0 green:190/255.0 blue:190/255.0 alpha:1.0]];
    self.bottomBtn.enabled = YES;

}

#pragma mark - 底部视图控件懒加载
/*
 @property (strong, nonatomic) UIButton *bottomBtn;
 @property (strong, nonatomic) UIImageView *iconView;
 @property (strong, nonatomic) UILabel *doctorNumLabel;
 @property (strong, nonatomic) UILabel *matchTitleLabel;
 */
- (UIButton *)bottomBtn {
    if (_bottomBtn == nil) {
        _bottomBtn = [[UIButton alloc] init];
        [_bottomBtn setTitle:@"就诊申请" forState:UIControlStateNormal];
        //[_bottomBtn  setBackgroundColor:[UIColor colorWithRed:50/255.0 green:190/255.0 blue:190/255.0 alpha:1.0]];
        [_bottomBtn setBackgroundColor:[UIColor lightGrayColor]];
        [_bottomBtn addTarget:self action:@selector(doctorRequest) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bottomBtn;
}

- (void)doctorRequest {
    
    OKDoctorSelectController *vc = [[OKDoctorSelectController alloc] init];

    NSDictionary *params = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};
    
    vc.params = params;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UIImageView *)iconView {
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] init];
        UIImage *image = [UIImage imageNamed:@"illness_img_person.png"];
        _iconView.image = image;
        _iconView.layer.cornerRadius = image.size.width/2.0;
    }
    return _iconView;
}

- (UILabel *)doctorNumLabel {
    if (_doctorNumLabel == nil) {
        _doctorNumLabel = [[UILabel alloc] init];
        _doctorNumLabel.text = @"0  位";
    }
    return _doctorNumLabel;
}

- (UILabel *)matchTitleLabel {
    if (_matchTitleLabel == nil) {
        _matchTitleLabel = [[UILabel alloc] init];
        _matchTitleLabel.text = @"重症诊所为您匹配到";
        _matchTitleLabel.textColor = [UIColor lightGrayColor];
    }
    return _matchTitleLabel;
}


@end







